<template>
<div>
  <h1>兑换记录</h1>
  <!---搜索表单-->
  <div class="search-div">
    <el-form label-width="70px" size="small">
      <el-row>
        <el-col :span="6">
          <el-form-item label="用户姓名">
              <el-input style="width: 300px" v-model="form.realname" placeholder="用户姓名"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="类型查询">
            <el-select v-model="form.cname">
              <el-option v-for="item in couponsTypes" :key="item.canme" :label="item.canme" :value="item.canme"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开始时间-结束时间">
            <el-date-picker
                v-model="createTimes"
                type="datetimerange"
                range-separator="To"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                format="YYYY-MM-DD HH:mm:ss"
                value-format="YYYY-MM-DD HH:mm:ss"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row style="display:flex">
        <el-button type="primary" size="small" @click="searchSysUser">
          搜索
        </el-button>
        <el-button size="small" @click="resetData">重置</el-button>
      </el-row>
    </el-form>
  </div>

  <!---数据表格-->
  <el-table :data="couponsUsers" style="width: 100%">
    <el-table-column prop="title" label="名称" />
    <el-table-column prop="canme" label="类型" />
    <el-table-column prop="time" label="兑换时间" />
    <el-table-column prop="realname" label="兑换人" />
    <el-table-column label="操作" align="center" width="280" #default="scope">
      <el-button type="danger" size="small" @click="deleteById(scope.row)">
        删除记录
      </el-button>
    </el-table-column>
  </el-table>
  <!--分页条-->
  <el-pagination
      v-model:current-page="pageParamsList.page"
      v-model:page-size="pageParamsList.limit"
      :page-sizes="[2, 20, 50, 100]"
      @size-change="fetchSysUser"
      @current-change="fetchSysUser"
      layout="total, sizes, prev, pager, next"
      :total="total"
  />
</div>
</template>

<script setup>

//搜索方法
import {getCouponsList, getCouponsTypes, getDeleteCouponsUser} from "@/api/changebattery/coupons";
import {onMounted, ref} from "vue";
import {ElMessage, ElMessageBox} from "element-plus";

//开始和结束时间数据模型
const createTimes = ref([])

const couponsTypes= ref([])

const queryDto = {
  createTimeBegin: '',
  createTimeEnd: '',
  cname:'',
  realname:''
}

const form = ref(queryDto)

//重置
const resetData = async () => {
  form.value.cname = ''
  form.value.createTimeBegin = ''
  form.value.createTimeEnd = ''
  form.value.realname=''
  createTimes.value=[]
  await fetchSysUser()
}

// 删除数据
const deleteById = row => {
  ElMessageBox.confirm('此操作将永久删除该记录, 是否继续?', 'Warning', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(async () => {
    const { code } = await getDeleteCouponsUser(row.cuid)
    if (code === 200) {
      ElMessage.success('删除成功')
      pageParamsUsers.value.page = 1
      await fetchSysUser()
    }
  })
}

//兑换记录
const couponsUser=ref([])

const searchSysUser = () => {
  fetchSysUser(
      pageParamsUsers.value.page,
      pageParamsUsers.value.limit,
      form.value
  )
}

//列表类型方法
const fetchCouponsTypes = async () => {
  const { data } = await getCouponsTypes()
  couponsTypes.value = data
}

//兑换记录
const couponsUsers=ref([])

//列表
const fetchSysUser = async () => {
  //获取开始和结束时间
  if (createTimes.value.length == 2) {
    form.value.createTimeBegin = createTimes.value[0]
    form.value.createTimeEnd = createTimes.value[1]
  }
  const { data } = await getCouponsList(
      pageParamsUsers.value.page,
      pageParamsUsers.value.limit,
      form.value
  )
  couponsUsers.value=data.couponsUser.records
  total.value = data.couponsUser.total
}
onMounted(async () => {
  await fetchSysUser()
  await fetchCouponsTypes()
})
//分页数据
const pageParamsList = {
  page: 1,
  limit: 10,
}
const total = ref(0)
const pageParamsUsers = ref(pageParamsList)
</script>

<style scoped>

</style>
